import wx

from GUIBudgetGrid import Table_1, Table_2, Table_3, Table_3_2, Table_4, Table_5, BudgetPanel

# 生成表三表四
# 测试将wx.MDIChildFrame 改为 wx.Panel
class BudgetGridNotebook(wx.MDIChildFrame):
   def __init__(self, parent, *args, **kwargs):
      # args 参数'表一' 至'表五' 顺序有别， 若为tuple [0] 为表，[1] 为自定义名字
      # data = {'表名字': [[...],[...]]} 在row 行 col 列 写入 data 数据
      # disable = {'表名字': [[...],[...]]} 在row 行 col 列 写入 'disable' 数据
      self.namedict = {
                  '表一':Table_1,
                  '表二':Table_2,
                  '表三':Table_3,
                  '表三乙':Table_3_2,
                  '表四':Table_4,
                  '表五':Table_5,
                  '表六':Table_4,
                  }
      data = kwargs.get('data', dict())
      dis = kwargs.get('disable', dict())
      Name = kwargs.get('windowname', '*')
      self.arg2list(args)
      super().__init__(parent, -1, Name)
      # super().__init__(parent)
      self.InitUI(data, dis)
      self.SetName(Name)

      Bitmap = wx.Bitmap()
      Icon = wx.Icon(Bitmap)
      self.SetIcon(Icon)

   def InitUI(self, data, dis): 
      # self.tablelist 为[(TableObject, Name)]
      sizer = wx.BoxSizer()   
      self.nb = wx.Notebook(self,style=wx.NB_BOTTOM|wx.NB_FIXEDWIDTH)
      for i in self.tablelist:
         self.nb.AddPage(BudgetPanel(self.nb, i[0], i[1]), i[1])
      sizer.Add(self.nb, 1, wx.EXPAND, 0)
      # self.setdisable(dis, self.nb)
      
      self.Centre()
      self.Show(True)
      self.SetSizer(sizer)
      self.Fit()

   def arg2list(self, data):
      self.tablelist = []
      for i in data:
         if isinstance(i, tuple):
            self.tablelist.append((self.namedict[i[0]](), i[1]))
         else:
            self.tablelist.append((self.namedict[i](), i))
      
   def setdata(self, data):
      if len(data) == 0:
         return
      tablelist = [i for i in self.nb.GetChildren()]
      tablenamelist = [i.name for i in tablelist]

      for d in data:
         if d in tablenamelist:
            tablelist[tablenamelist.index(d)].setdata(data[d])

   def getdata(self, tablename):
      return

   def getgrid(self, getname):
      tablelist = [i for i in self.nb.GetChildren()]
      tablenamelist = [i.name for i in tablelist]

      for index, name in enumerate(tablenamelist):
         if name == getname:
            return tablelist[index].getgrid()

   def getpanel(self, getname):
      tablelist = [i for i in self.nb.GetChildren()]
      tablenamelist = [i.name for i in tablelist]

      for index, name in enumerate(tablenamelist):
         if name == getname:
            return tablelist[index].getpanel()

   def GetData(self, name):
      tablelist = [i for i in self.nb.GetChildren()]
      tablenamelist = [i.name for i in tablelist]

      data = None
      for n in tablenamelist:
         if n == name:
            data = tablelist[tablenamelist.index(n)].GetData()
      
      return data
   
   def SetData(self, data, name):
      tablelist = [i for i in self.nb.GetChildren()]
      tablenamelist = [i.name for i in tablelist]
      grid = self.getgrid(name)
      gridnumrows = grid.GetNumberRows()
      datanumrows = len(data)
      if gridnumrows < datanumrows:
         grid.AppendRows(datanumrows - gridnumrows)
      elif gridnumrows > datanumrows:
         grid.DeleteRows(0, gridnumrows - datanumrows)

      for n in tablenamelist:
         if n == name:
            data = tablelist[tablenamelist.index(n)].SetData(data)


   # def setrownum(self, num, name):
   #    tablelist = [i for i in self.nb.GetChildren()]
   #    tablenamelist = [i.name for i in tablelist]
   #    for n in tablenamelist:
   #       if name == n:
   #          tablelist[tablenamelist.index(name)].setrownum(num)

if __name__ == "__main__":
   class TestFrame(wx.Frame):
      def __init__(self, parent, title):
         super().__init__(parent, -1, title, size = (1000,600))  
         self.InitUI()

      def InitUI(self):    
         # sizer = wx.BoxSizer()   
         data={'我的表一':[[22,'=$(0,0)$*0.17'],[22,'=$(0,0)$*0.17','=$(0,0)$*0.17','=$(0,0)$*0.17','=$(0,0)$*0.17']]}
         disable={'我的表一':[['','']]}
         panel = BudgetGridNotebook(self, ('表一', '我的表一'), '表二', '表三', '表四')
         panel.setdata(data)
         # sizer.Add(panel, wx.EXPAND)
         # self.SetSizer(sizer)
         # self.Fit()
      # sizer = wx.BoxSizer()
      # sizer.Add(panel, wx.EXPAND)
      # self.SetSizer(sizer)
      # self.Fit()

   app = wx.App()
   frame = TestFrame(None, 'test')
   frame.Show()
   app.MainLoop()